/*
 * @lc app=leetcode.cn id=13 lang=csharp
 *
 * [13] 罗马数字转整数
 */

// @lc code=start
public class Solution {
    static Dictionary<char, int> dic = new Dictionary<char, int>
    {
        {'I', 1},
        {'V', 5},
        {'X', 10},
        {'L', 50},
        {'C', 100},
        {'D', 500},
        {'M', 1000}
    };
    public int RomanToInt(string s) {
        int result = 0;
        var count = s.Length - 1;
        for(int i = 0; i < s.Length; i++)
        {
            var c = s[i];
            if(i < count)
            {
                var next = s[i + 1];
                if(c == 'I' && (next == 'V' || next == 'X')
                || c == 'X' && (next == 'L' || next == 'C')
                || c == 'C' && (next == 'D' || next == 'M'))
                {
                    result -= dic[c];
                }
                else
                {
                    result += dic[c];
                }
            }
            else
            {
                result += dic[c];
            }
        }
        return result;
    }
}
// @lc code=end

